/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.domain.content;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.Domain;
import han.bifang.gwt.client.common.util.StringUtils;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.domain.DomainContentConstants;
import han.bifang.gwt.client.domain.DomainMessages;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

public class CreateOrModifyOrRemoveDomainContent extends Content {
	public static final String CREATE = "create";
	public static final String MODIFY = "modify";
	public static final String REMOVE = "remove";

	private DomainMessages cons;
	private BifangGWTServiceAsync service;
	private boolean isLocal;
	private String action;

	private TextBox name = null;
	private TextArea desc = null;

	private Domain d = null;

	private AsyncCallback<ResponseObject> createcb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}
			ArrayList a = (ArrayList) res.getResponse();
			String treeId = (String) a.get(0);
			String dId = (String) a.get(1);
			d.setId(dId);
			d.setUnitId(treeId);
			HashMap p = new HashMap();
			p.put("domain", d);
			listener.onContentChanged(DomainContentConstants.CREATE_DOMAIN,
					DomainContentConstants.DOMAIN_ROOT, p);

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}

	};
	
	private AsyncCallback<ResponseObject> modifycb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}
			
			HashMap p = new HashMap();
			p.put("domain", d);
			listener.onContentChanged(DomainContentConstants.MODIFY_DOMAIN,
					DomainContentConstants.DOMAIN_DETAIL, p);

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}

	};
	
	private AsyncCallback<ResponseObject> removecb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}
			
			HashMap p = new HashMap();
			p.put("domain", d);
			listener.onContentChanged(DomainContentConstants.REMOVE_DOMAIN,
					DomainContentConstants.DOMAIN_ROOT, p);

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}

	};

	public CreateOrModifyOrRemoveDomainContent(DomainMessages cons,
			BifangGWTServiceAsync service, boolean local, String action,
			Domain domain) {
		this.cons = cons;
		this.service = service;
		this.isLocal = local;
		this.action = action;
		this.d = domain;

		VerticalPanel panel = new VerticalPanel();
		panel.setWidth("100%");
		//panel.setSpacing(5);
		
		
		Button c = null;
		
		Grid table = new Grid(2, 2);
		table.setWidth("80%");
		ColumnFormatter cf = table.getColumnFormatter();
		cf.setWidth(0, "15%");
		cf.setWidth(1, "45%");
		
		table.setCellPadding(1);
		table.setCellSpacing(1);
		table.setBorderWidth(1);

		table.setHTML(0, 0, cons.name());
		table.setHTML(1, 0, cons.desc());

		CellFormatter ff = table.getCellFormatter();
		ff.setStyleName(0,0, "tablecol");
		ff.setStyleName(1,0, "tablecol");
		

		if (action.equals(CREATE)) {
			ff.setStyleName(0,0, "tablecol");
			name = new TextBox();
			
			 name.setVisibleLength(32);

			desc = new TextArea();
			desc.setCharacterWidth(64);
			desc.setVisibleLines(6);

			table.setWidget(0, 1, name);
			table.setWidget(1, 1, desc);

			c = new Button(cons.create());
			c.addClickListener(new ClickListener() {
				public void onClick(Widget sender) {
					if (!validate()) {
						return;
					}
					d = new Domain();

					d.setLocal(isLocal);

					d.setName(name.getText());
					d.setDescription(desc.getText());
					RequestObject req = new RequestObject();
					req
							.setAction("han.bifang.gwt.server.action.domain.CreateDomainAction");
					req.setRequest(d);
					callService(req);
				}
			});
			HTML h = new HTML(cons.domainBasic());
			panel.add(h);
			panel.add(table);
			panel.add(c);
		}
		
		else if(action.equals(MODIFY)){
			
			name = new TextBox();
			name.setVisibleLength(32);
			name.setText(d.getName());

			desc = new TextArea();
			desc.setCharacterWidth(64);
			desc.setVisibleLines(6);
			desc.setText(d.getDescription());

			table.setWidget(0, 1, name);
			table.setWidget(1, 1, desc);

			c = new Button(cons.modify());
			c.addClickListener(new ClickListener() {
				public void onClick(Widget sender) {
					if (!validate()) {
						return;
					}
					
					d.setName(name.getText());
					d.setDescription(desc.getText());
					RequestObject req = new RequestObject();
					req
							.setAction("han.bifang.gwt.server.action.domain.ModifyDomainAction");
					req.setRequest(d);
					callService(req);
				}
			});
			HTML h = new HTML(cons.domainBasic());
			panel.add(h);
			panel.add(table);
			panel.add(c);
		}
		
		else if(action.equals(REMOVE)){

			
			table.setHTML(0 ,1,d.getName());
			table.setHTML(1, 1, d.getDescription());
			

			c = new Button(cons.remove());
			c.addClickListener(new ClickListener() {
				public void onClick(Widget sender) {
					
					
					RequestObject req = new RequestObject();
					req
							.setAction("han.bifang.gwt.server.action.domain.RemoveDomainAction");
					req.setRequest(d.getId());
					callService(req);
				}
			});
			
			Button cancel = new Button(cons.cancel());
			cancel.addClickListener(new ClickListener() {
				public void onClick(Widget sender) {
					HashMap p = new HashMap();
					
					listener.onContentChanged(DomainContentConstants.REMOVE_DOMAIN,
							DomainContentConstants.DOMAIN_DETAIL, p);
					
				}
			});
			
			
			HTML h = new HTML(cons.domainBasic());
			panel.add(h);
			panel.add(table);
			HTML l = new HTML(cons.confirmRemove());
			panel.add(l);
			HorizontalPanel hp = new HorizontalPanel();
			hp.add(c);
			hp.add(cancel);
			panel.add(hp);
		}
		
		initWidget(panel);

	}

	private void callService(RequestObject req) {
		if(action.equals(CREATE))
			service.call(req, createcb);
		else if(action.equals(MODIFY)){
			service.call(req, modifycb);
			
		}else if(action.equals(REMOVE)){
			service.call(req, removecb);
			
		}
	}

	private boolean validate() {
		if (!StringUtils.checkLenght(name.getText(), 1, 128)) {
			Window.alert(cons.lengthError(cons.name(), "1", "128"));
			return false;
		}
		if (!StringUtils.checkLenght(desc.getText(), 1, 1024)) {
			Window.alert(cons.lengthError(cons.desc(), "1", "1024"));
			return false;

		}
		return true;

	}

}
